<template>
  <div class="app-container">
    <div class="top">
      <div class="left"><img src="@/assets/images/icon/leftIcon.png" alt="" srcset=""
          style="width: 15px;height: 14px;">模板库</div>
      <div class="right">
        <el-button type="primary" size="mini" @click="add()" v-hasPermi="['system:template:insert']">新增</el-button>
      </div>
    </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="120px">
      <el-form-item label="请输入名称搜索：" prop="ipaddr" label-width="130px">
        <el-input v-model="search.name" placeholder="名称搜索" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="人员：" prop="nickName">
        <el-select v-model="search.createBy" placeholder="选择人员">
          <el-option :label="i.nickName" :value="i.userId" v-for="i in userList"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模板类型" :label-width="formLabelWidth" label-position="left">
        <el-select v-model="search.templateTypeId" placeholder="请选择模板类型" style="width: 100%;">
          <el-option :label="i.name" :value="i.templateTypeId" v-for="i in typeList"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list.slice((pageNum - 1) * pageSize, pageNum * pageSize)" style="width: 100%;">
      <!-- <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="模板名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="模板类型" align="center" prop="templateTypeName" :show-overflow-tooltip="true" />
      <el-table-column label="隶属人员" align="center" prop="affiliationName" :show-overflow-tooltip="true" />
      <!-- <el-table-column label="" align="center" prop="loginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="edit(scope.row)"
            v-hasPermi="['system:template:query']">详情</el-button>
          <span class="delete"> <el-button size="mini" type="text" @click="handleForceLogout(scope.row)"
              v-hasPermi="['system:template:remove']">删除</el-button></span>
          <!-- <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['system:template:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="search.pageNum" :limit.sync="search.pageSize"
      @pagination="getList" />

    <el-dialog title="新增模板" :visible.sync="dialogFormVisible" width="30%" center>
      <el-form :model="form">
        <el-form-item label="模板名称" :label-width="formLabelWidth" label-position="left">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="模板类型" :label-width="formLabelWidth" label-position="left">
          <el-select v-model="form.parentId" placeholder="请选择模板类型" style="width: 100%;">
            <el-option :label="i.name" :value="i.templateTypeId" v-for="i in typeList"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <div class="left"></div>
        <div class="right"> <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="insertTemplate">确 定</el-button>

        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { insertTemplate, listType, list, deleteTemplateItem, getUser } from "@/api/template/template";
import { Message } from 'element-ui'
export default {
  name: "Online",
  data() {
    return {
      dialogFormVisible: false,
      form: {
        "parentId": "",
        "name": ""
      },
      userList: [],
      search: {
        templateTypeId: '',
        createBy: '',
        name: '',
        pageNum: 1,
        pageSize: 10,
      },
      infolist: [{ name: '选项内容' }],
      formLabelWidth: '100px',
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      userParams: {
        pageNum: 1,
        pageSize: 10,
      },
      typeList: []
    };
  },
  created() {
    this.getList();
    this.getUser()
    this.searchType()//获取模板类型
  },
  methods: {
    async getUser() {
      let res = await getUser(this.userParams)
      this.userList = res.data
      console.log(this.userList, 'getUser')
    },
    async searchType() {
      let res = await listType({ name: '' })
      this.typeList = res.rows
      console.log(this.typeList, 11)
    },
    async insertTemplate() {
      // console.log(this.form, '11')
      let data = {
        name: this.form.name,
        templateTypeId: this.form.parentId,
        templateTypeName: this.form.parentId.name
      }
      let res = await insertTemplate(data)
      Message({ message: '添加成功', type: 'success' })
      this.form = {
        "parentId": "",
        "name": ""
      };
      this.getList()
      this.dialogFormVisible = false
    },
    add() {
      this.dialogFormVisible = true
    },
    closeItem(e) {
      this.infolist.splice(e, 1)
    },
    addSub() {
      this.infolist.push({ name: '选项内容' })
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.search).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.search.name = '';
      this.search.createBy = '';
      this.search.templateTypeId = '';
      this.resetForm("queryForm");
      this.handleQuery();
    },
    edit(row) {
      this.$router.push({
        path: '/templateLibrary/templateLibrary/details',
        query: {
          templateId: row.templateId,
          templateName: row.name
        }
      })
    },
    /** 删除 */
    handleForceLogout(row) {
      this.$modal.confirm('是否删除').then(function () {
        return deleteTemplateItem([row.templateId]);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("是否删除");
      }).catch(() => { });
    }
  }
};
</script>

<style scoped lang="scss">
::v-deep .delete .el-button--text {
  color: #FF4431 !important;
  margin-left: 10px;
}

.top {
  display: flex;
  justify-content: space-between;
  color: #333;
  margin-bottom: 15px;

  .left {
    img {
      margin-right: 10px;
    }
  }

  .right {}
}

.dialog-footer {
  display: flex;
  justify-content: space-between;
}

.item {
  display: flex;
  align-items: center;
  position: relative;
  top: -10px;
  left: 10px;

  .close {
    position: relative;
    top: -10px;
    left: 10px;
    font-size: 20px;
    cursor: pointer;
  }
}
</style>
